<?php 
class UserDetails extends User
{
	public $Name;
	public $Lastname;
	public $Company_id;
	public $Gender;
	public $Bdate;
	public $Country;
	public $State;

	public $City;
	public $Zip;
	public $Address;
	public $Address2;
	public $Phone;
	public $Mob;
	public $Fax;
	public $Www;
	public $Valid_customer;
	public $Online_time;
	public $DetailsExist; 
	
	public $AlternateDelivery;
	public $DeliveryName;
	public $DeliveryLastName;
	public $DeliveryAddress;
	public $DeliveryAddress2;
	public $DeliveryZip;
	public $DeliveryCity;
	public $DeliveryCountry;
	
	public $ProfilePage;
	
	public function getCompany() { return $this->Company; }
	public function getCompany_id() { return $this->Company_id; }
	public function getGender() { return $this->Gender; }
	public function getBdate() { return $this->Bdate; }
	public function getZip($minLen=0) 
	{ 
		if($minLen > 0)
		{
			if(strlen($this->Zip) >= $minLen)
			{ return $this->Zip; }
			else
			{
				$newZip = $this->Zip;
				$nowlen = strlen($newZip);
				while($nowlen < $minLen)
				{
					$newZip = '0'.$newZip;
					$nowlen = strlen($newZip);
				}
				return $newZip;
			}
		}
		else
		{ return $this->Zip; }
	}
	public function getPhone() { return $this->Phone; }
	public function getValid_customer() { return $this->Valid_customer; }
	public function getOnline_time() { return $this->Online_time; }
	
	public function getAlternateDelivery() { return intval($this->AlternateDelivery); }
	public function getDeliveryName() 
	{
		if(($this->DeliveryName != '') || (self::getAlternateDelivery() > 0))
		{ return $this->DeliveryName; }
		else
		{ return $this->Name; }
	}
	public function getDeliveryLastName() 
	{ 
		if(($this->DeliveryLastName != '') || (self::getAlternateDelivery() > 0))
		{ return $this->DeliveryLastName; }
		else
		{ return $this->Lastname; }
	}
	public function getDeliveryAddress() 
	{ 
		if(($this->DeliveryAddress != '') || (self::getAlternateDelivery() > 0))
		{ return $this->DeliveryAddress; }
		else
		{ return $this->Address; }
	}
	public function getDeliveryAddress2() 
	{ 
		if(($this->DeliveryAddress2 != '') || (self::getAlternateDelivery() > 0))
		{ return $this->DeliveryAddress2; }
		else
		{ return $this->Address2; }
	}
	public function getDeliveryZip($minLen = 0) 
	{ 
		if(!(($this->DeliveryZip != '') || (self::getAlternateDelivery() > 0)))
		{ $this->DeliveryZip = $this->Zip; }
		if($minLen > 0)
		{
			if(strlen($this->DeliveryZip) >= $minLen)
			{ return $this->DeliveryZip; }
			else
			{
				$newZip = $this->DeliveryZip;
				$nowlen = strlen($newZip);
				while($nowlen < $minLen)
				{
					$newZip = '0'.$newZip;
					$nowlen = strlen($newZip);
				}
				return $newZip;
			}
		}
		else
		{ return $this->DeliveryZip; }
	}
	public function getDeliveryCity() 
	{ 
		if(($this->DeliveryCity != '') || (self::getAlternateDelivery() > 0))
		{ return $this->DeliveryCity; }
		else
		{ return $this->City; }
	}
	public function getDeliveryCountry() 
	{
		if(($this->DeliveryCountry != '') || (self::getAlternateDelivery() > 0))
		{ return $this->DeliveryCountry; }
		else
		{ return $this->Country; }
	}
	
	public function setCompany_id($val) { $this->Company_id=$val; }
	public function setGender($val) { $this->Gender=$val; }
	public function setBdate($val) { $this->Bdate=$val; }
	public function setZip($val) { $this->Zip=$val; }
	public function setPhone($val) { $this->Phone=$val; }
	public function setValid_customer($val) { $this->Valid_customer=$val; }
	public function setOnline_time($val) { $this->Online_time=$val; }
	
	public function setAlternateDelivery($val) { $this->AlternateDelivery=intval($val); }
	
	public function UserDetails($user) 
	{ 
		self::setDataTable('user_details');
		if($user)
		{ 
			self::setId($user->getId());
			self::setUsername($user->getUsername());
			self::setEmail($user->getEmail());
			self::setPassword($user->getPassword());
			self::setLevel($user->getLevel());
			self::setImage($user->getImage());
			self::setImageObject($user->getImageObject());
			
			self::setCreditsBought($user->getCreditsBought());
			self::setCreditsFree($user->getCreditsFree());
			self::setCreditsUsed($user->getCreditsUsed());
			
			self::setPartnerStatus($user->getPartnerStatus());
			self::setPartnerStatusDateStart($user->getPartnerStatusDateStart());
			self::setPartnerTypes($user->getPartnerTypes());
			$this->DetailsExist = false;
			self::loadDetails(); 
		}
	}
	
	public function loadDetails()
	{
		if(self::getId() > 0)
		{
			$select = 'SELECT * FROM '.DB_PERFIX.'user_details WHERE id='.$this->Id.' LIMIT 1;';
			$result = mysql_query($select);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{	 	 	 	 	 	 	 	 	 	 	 	 	 	
					self::setName($row['name']);
					self::setLastname($row['lastname']);
					self::setCompany($row['company']);
					self::setCompany_id($row['company_id']);
					self::setGender($row['gender']);
					self::setBdate($row['bdate']);
					self::setCountry($row['country']);
					self::setState($row['state']);
					self::setCity($row['city']);
					self::setZip($row['zip']);
					self::setAddress($row['address']);
					self::setAddress2($row['address2']);
					self::setPhone($row['phone']);
					self::setMob($row['mob']);
					self::setFax($row['fax']);
					self::setWww($row['www']);
					self::setValid_customer($row['valid_customer']);
					self::setOnline_time($row['online_time']);
					self::setAlternateDelivery($row['alternate_delivery']);
					self::setDeliveryName($row['delivery_name']);
					self::setDeliveryLastName($row['delivery_last_name']);
					self::setDeliveryAddress($row['delivery_address']);
					self::setDeliveryAddress2($row['delivery_address2']);
					self::setDeliveryZip($row['delivery_zip']);
					self::setDeliveryCity($row['delivery_city']);
					self::setDeliveryCountry($row['delivery_country']);
					self::setProfilePage($row['ProfilePage']);
					if($row['id'] > 0)
					{ $this->DetailsExist = true; }
				}
			}
			if($this->DetailsExist == false)
			{
				$sql = 'INSERT INTO '.DB_PERFIX.'user_details (`id`, `valid_customer`) VALUES ('.$this->Id.', 0);';
				//echo $sql;
				$result = mysql_query($sql);
			}
			
		}
	}
	
	public function saveDetails()
	{
		global $txt;
		if(self::getId() > 0)
		{
			self::saveUser();
			$sql  = 'UPDATE '.DB_PERFIX.'user_details SET ';
			$sql .= '`name`="'.$txt->formatText(self::getName()).'", ';
			$sql .= '`lastname`="'.$txt->formatText(self::getLastname()).'", ';
			$sql .= '`company`="'.$txt->formatText(self::getCompany()).'", ';
			$sql .= '`company_id`="'.self::getCompany_id().'", ';
			if($this->Bdate != '')
			{ $sql .= '`bdate`="'.self::getBdate().'", '; }
			$sql .= '`country`="'.self::getCountry().'", ';
			if($this->State > 0)
			{ $sql .= '`state`="'.self::getState().'", '; }
			$sql .= '`city`="'.$txt->formatText(self::getCity()).'", ';
			$sql .= '`zip`="'.self::getZip().'", ';
			$sql .= '`address`="'.$txt->formatText(self::getAddress()).'", ';
			$sql .= '`address2`="'.$txt->formatText(self::getAddress2()).'", ';
			$sql .= '`phone`="'.self::getPhone().'", ';
			
			$sql .= '`mob`="'.self::getMob().'", ';
			$sql .= '`fax`="'.self::getFax().'", ';
			$sql .= '`www`="'.self::getWww().'", ';
			$sql .= '`alternate_delivery`='.self::getAlternateDelivery().', ';
			
			$sql .= '`delivery_name`="'.$txt->formatText(self::getDeliveryName()).'", ';
			$sql .= '`delivery_last_name`="'.$txt->formatText(self::getDeliveryLastName()).'", ';
			$sql .= '`delivery_address`="'.$txt->formatText(self::getDeliveryAddress()).'", ';
			$sql .= '`delivery_address2`="'.$txt->formatText(self::getDeliveryAddress2()).'", ';
			$sql .= '`delivery_zip`="'.self::getDeliveryZip().'", ';
			$sql .= '`delivery_city`="'.$txt->formatText(self::getDeliveryCity()).'", ';
			$sql .= '`delivery_country`="'.$txt->formatText(self::getDeliveryCountry()).'", ';
			$sql .= '`ProfilePage`="'.$txt->formatText(self::getProfilePage()).'", ';
			
			$sql .= '`valid_customer`='.self::getValid_customer().'  ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';	 
			$result = mysql_query($sql);
		}
		return;
	}
}
?>